﻿using System;
using System.Collections.Generic;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SpriteGame.Rendering;
using System.Xml.Serialization;

namespace SpriteGame.Entities
{
    class AnimatedSprite:BaseSprite
    {
        AnimatedSprite(Scene _parent)
            : base(_parent)
        {
        }

        public AnimatedSprite(Scene _parent, BodyType _type, Vector2 _position, float _rotation, String _texture)
            : base(_parent, _type, _position, _rotation, _texture)
        {
            CanSave = false;
        }

        int accum = 0;
        int numFrames = 18;
        public int NumFrames
        {
            get { return numFrames; }
            set { numFrames = value; }
        }

        int currentFrame = 0;
        public int CurrentFrame
        {
            get { return currentFrame; }
            set { currentFrame = value; }
        }

        bool animating = true;

        public bool Animating
        {
            get { return animating; }
            set { animating = value; }
        }


        public override void LoadContent(Scene _parent)
        {
            //base.LoadContent(_parent);
            parent = _parent;
            world = _parent.World;

            Texture2D tex = TextureLibrary.GetTexture(texture);

            //body.
            width = (float)tex.Width;
            width /= (float)numFrames;
            width *= 0.07f;
            height = (float)tex.Height * 0.07f;



            float hx = width * 0.5f;
            float hy = height * 0.5f;
            Vertices box = PolygonTools.CreateRectangle(hx, hy);

            PolygonShape shape = new PolygonShape(box);

            body = parent.World.CreateBody();
            body.SetTransform(ref startPos, startRot);
            body.BodyType = BodyType;
            Fixture fixture = body.CreateFixture(shape, Mass);

            //assign this entity to the physics body's tag, 
            //this means we can use the physics engine for visual culling
            //yay less work :D
            body.UserData = this;
            isLoaded = true;
        }

        public override void Draw(ShapeRenderer _batch)
        {
            if (animating)
            {
                if (accum > 2)
                {
                    accum = 0;
                    currentFrame++;
                    currentFrame = currentFrame % numFrames;
                }
            }

            accum++;

            bool flip = false;

            if (body.LinearVelocity.X > 0)
            {
                flip = true;
            }

            _batch.DrawAnimatedQuad(numFrames, currentFrame, Position, Rotation, Width, Height, TextureLibrary.GetTexture(Texture), !flip);

            //base.Draw(_batch);
        }
    }
}
